#include<iostream>
#include<queue>
#define ll long long
using namespace std;
typedef struct point{
	ll next[6207];
	ll weight[6207];
	ll tail;
}Point;
Point a[2507];
ll book[2507];
queue<ll>q;
queue<ll>len;
int main(){
	ll n,m,s,t,u,v,w,now,i,sum=0,ans=1e15;
	cin>>n>>m>>s>>t;
	while(m--){
		cin>>u>>v>>w;
		a[u].next[a[u].tail] = v;
		a[v].next[a[v].tail] = u;
		a[u].weight[a[u].tail++]=w;
		a[v].weight[a[v].tail++]=w;
	}
	q.push(s);
	len.push(0);
	while(!q.empty()){
		now=q.front();
		book[now]=1;
		sum=len.front();
		for(i=0;i<a[now].tail;i++){
			if(!book[a[now].next[i]]){
				if(a[now].next[i]==t){
					ans=min(ans,sum+a[now].weight[i]);
				}
				q.push(a[now].next[i]);
				len.push(sum+a[now].weight[i]);
			}
		}
		q.pop();
		len.pop();
	}
	cout<<ans<<endl;
	return 0;
} 
